package com.xieyingwangluo.yxpart.api;

import com.alibaba.fastjson.JSON;
import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.entity.UserModel;
import com.xieyingwangluo.yxpart.logic.UserLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Api(tags = "user接口")
@Controller
@RequestMapping(value = "/api/v1/user")
public class UserBaseRest extends BaseRest {
    @Autowired
    private UserLogic userLogic;

    //注册
    @ApiOperation(value = "新增user", notes = "post Rest接口的user方法", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "主键", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "tel", value = "电话", required = true, dataType = "String", paramType = "form", defaultValue = "17689989986"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "form", defaultValue = "123456"),
            @ApiImplicitParam(name = "name", value = "昵称", required = true, dataType = "String", paramType = "form", defaultValue = "小马哥"),
            @ApiImplicitParam(name = "head_img", value = "头像", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "intg", value = "积分", required = false, dataType = "int", paramType = "form", defaultValue = "600"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, dataType = "int", paramType = "form", defaultValue = "1"),
            @ApiImplicitParam(name = "vip", value = "会员", required = false, dataType = "int", paramType = "form", defaultValue = "1"),
            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "form")
    })
    @ResponseBody
    @PostMapping(value = "/add")
    public Result add(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                      UserModel userModel) {
        return userLogic.add(userModel);
    }


    @ApiOperation(value = "普通用户登录", notes = "普通用户登录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "电话", required = true, dataType = "String", paramType = "query", defaultValue = "17689989986"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query", defaultValue = "123456")
    })
    @ResponseBody
    @GetMapping(value = "/checklogin")
    public Result checklogin(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                             HttpSession session, @RequestParam("tel") String tel, @RequestParam("pwd") String pwd) {
        return userLogic.checklogin(session, tel, pwd);
    }

    //普通用户的退出
    @ApiOperation(value = "普通用户的退出", notes = "普通用户的退出", produces = "application/json")
    @ApiImplicitParams({
    })
    @ResponseBody
    @GetMapping(value = "/outlogin")
    public Result outlogin(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                           HttpSession session
    ) {
        return userLogic.outlogin(session);
    }

    @ApiOperation(value = "更新user密码", notes = "post Rest接口的user方法", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "登录用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "oldpwd", value = "旧密码", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @PostMapping(value = "/updPwd")
    public Result updatePassword(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                                 @RequestParam("noid") String noid,
                                 @RequestParam("pwd") String pwd,
                                 @RequestParam("oldpwd") String oldpwd) {
        return userLogic.updPwd(noid, pwd, oldpwd);
    }

    //更新user
//    @ApiOperation(value = "更新user", notes = "post Rest接口的user方法", produces = "application/json")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "noid", value = "用户id", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "tel", value = "电话", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "name", value = "昵称", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "head_img", value = "头像", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "intg", value = "积分", required = true, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "state", value = "状态", required = true, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "vip", value = "会员", required = true, dataType = "int", paramType = "query"),
//            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "query")
//    })
//    @ResponseBody
//    @PostMapping(value = "/upd")
//    public Result update(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
//                         UserModel userModel) {
//        return userLogic.update(userModel);
//    }


    @ApiOperation(value = "获取数据查询接口", notes = "Get Rest接口的user方法", produces = "application/json")
    @ApiImplicitParam(name = "noid", value = "用户表noid", required = true, dataType = "String", paramType = "query", defaultValue = "ba655842-a519-11e8-b753-000c2984c921")
    @ResponseBody
    @GetMapping(value = "/selO")
    public Result getById(@RequestHeader(name = "token", defaultValue = "token") String token,
                          String noid) {
        return userLogic.getById(noid);
    }

    //模糊查询用户信息：商户编号(noid)
    @ApiOperation(value = "模糊查询用户信息", notes = "模糊查询用户信息", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "主键", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "tel", value = "电话", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "昵称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "head_img", value = "头像", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "intg", value = "积分", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "vip", value = "会员", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "create_time", value = "创建时间", required = false, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @GetMapping(value = "/selA")
    public Result selA(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                       UserModel userModel
    ) {
        return userLogic.selA(userModel);
    }

    @ApiOperation(value = "设置用户头像", notes = "设置当前用户头像", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "登录用户id", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @PutMapping(value = "/updImg")
    public Result updImg(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                         @RequestParam(required = true) MultipartFile profile,
                         @RequestParam("noid") String noid) {
        return userLogic.updImg(profile, noid);
    }

    @ApiOperation(value = "拉黑用户", notes = "拉黑用户", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noid", value = "用户id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(0拉黑，1未拉黑)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "info", value = "违规描述记录", required = true, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @PutMapping(value = "/updS")
    public Result updS(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                       @RequestParam("noid") String noid,
                       @RequestParam("state") int state,
                       @RequestParam("info") String info) {
        return userLogic.updS(noid, state, info);
    }

    //查询黑名单
    @ApiOperation(value = "查询黑名单", notes = "查询黑名单", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态(0拉黑，1未拉黑)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "用户昵称", required = false, dataType = "String", paramType = "query")
    })
    @ResponseBody
    @GetMapping(value = "/selS")
    public Result selS(@RequestHeader(name = "Authorization", defaultValue = "token") String token,
                       @RequestParam("state") int state,
                       @RequestParam(value = "name", required = false) String name) {
        return userLogic.selS(state, name);
    }
}
